package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw;

import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.common.OtwConstant;
import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.dto.InOutBoundOrderDetailReq;
import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.otw.dto.InOutBoundOrderReq;
import com.alibaba.cirtus.ots.application.adapter.ability.model.Result;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpTokenUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.HttpUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import com.epoch.app.otsapplicationadaptersdk.mnotwif006.service.MnOtwIf006Service;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnOtwCommonResponse;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MnOtwIf006Request;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author baizhuo
 * @version 1.0
 * @date 2023/12/21 10:45
 */
@Component
@Primary
public class MnOtwIf006ServiceImpl implements MnOtwIf006Service {

    private final Log log = Log.getLogger(MnOtwIf006ServiceImpl.class);

    @Autowired
    private HttpUtils httpUtils;

    @Override
    @ProcessInvoker(remark = "MnOtwIf006Service", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public MnOtwCommonResponse inOutBoundOrderCreateSync(MnOtwIf006Request mnOtwIf006Request) {
        log.info("MnOtwIf006Service inOutBoundOrderCreateSync param:{}", JSON.toJSONString(mnOtwIf006Request));
        Map<String, String> header = HttpTokenUtil.generateHttpHeader();
        String urlPrefix = OtsApplicationAdapterSdkConstants.urlPrefix();
        String url = urlPrefix + "api/mn_bc_link/inOutBoundOrderCreate";
        MnOtwCommonResponse mnOtwCommonResponse = new MnOtwCommonResponse();
        try {
            InOutBoundOrderReq inOutBoundOrderReq = new InOutBoundOrderReq();
            List<InOutBoundOrderDetailReq> detailReqList = mnOtwIf006Request.getDetailList().stream().map(detail -> {
                InOutBoundOrderDetailReq inOutBoundOrderDetailReq = new InOutBoundOrderDetailReq();
                BeanUtils.copyProperties(detail, inOutBoundOrderDetailReq);
                return inOutBoundOrderDetailReq;
            }).collect(Collectors.toList());
            BeanUtils.copyProperties(mnOtwIf006Request, inOutBoundOrderReq);
            inOutBoundOrderReq.setDetailList(detailReqList);
            log.info("inOutBoundOrderCreateSync http header:{}, url:{}, param:{}", header, url, JSON.toJSONString(inOutBoundOrderReq));
            ResponseEntity<Result> post = httpUtils.post(url, header, inOutBoundOrderReq, Result.class);
            Result result = post.getBody();
            log.info("MnOtwIf006Service inOutBoundOrderCreate mnOtwCommonResponse result:{}", JSON.toJSONString(result));
            if (result.isSuccess()) {
                mnOtwCommonResponse.setSuccess(OtwConstant.SUCCESS_TRUE);
                mnOtwCommonResponse.setResult(result.getResult());
            } else {
                mnOtwCommonResponse.setSuccess(OtwConstant.SUCCESS_FALSE);
                mnOtwCommonResponse.setErrorCode(result.getErrorCode());
                mnOtwCommonResponse.setMessage(result.getMessage());
            }
        } catch (Exception e) {
            log.error("call bc-link inOutBoundOrderCreate error", e);
            mnOtwCommonResponse.setSuccess(OtwConstant.SUCCESS_FALSE);
            mnOtwCommonResponse.setErrorCode(OtwConstant.ORDER_001_01_01_001);
            mnOtwCommonResponse.setMessage(OtwConstant.COMMON_MESSAGE);
        }
        return mnOtwCommonResponse;
    }
}
